﻿using System.Collections.Generic;
using System.IO;
using Plugins.StateMachines;

namespace Plugins.Samples
{
    public static class DirectoryStateMachine
    {
        public class FileState : PluginState<FileInfo>
        {
            public FileState(PluginBaseState handler, Factory<IEnumerable<FileInfo>> factory) : base(handler, factory) { }
            public override bool Run()
            {
                foreach (FileInfo info in Values)
                {
                    SetNextValue(info);
                    return true;
                }
                return base.Run();
            }
        }
        public class DirectoryState : PluginState<DirectoryInfo>
        {
            public DirectoryState(PluginBaseState handler, Factory<IEnumerable<DirectoryInfo>> factory) : base(handler, factory) { }
            public override bool Run()
            {
                foreach (DirectoryInfo info in Values)
                {
                    SetNextState(new InitialState(this, info));
                    return RunNextState();
                }
                return base.Run();
            }
        }
        public class InitialState : PluginState<FileInfo>
        {
            private DirectoryInfo _directory;
            public InitialState(PluginBaseState handler, DirectoryInfo directory)
                : base(handler, () => directory.GetFiles())
            {
                this._directory = directory;
            }
            public override bool Run()
            {
                SetNextValue(_directory);
                SetNextState(new FileState(new DirectoryState(CurrentState, () => _directory.GetDirectories()), () => Values));
                return true;
            }
        }
    }
}
